<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

require_once dirname(__FILE__).DS.'helper'.DS.'jafunctions.class.php';
function JajobboardBuildRoute(&$query)
{
	$segments = array();
    
	// get a menu item based on Itemid or currently active
	$menu = &JSite::getMenu();
	if(isset($query['view']) && $query['view']=='jaresumes'){
		$query['Itemid'] = null;    
	}
	if (empty($query['Itemid'])) {
		$menuItem = &$menu->getActive();
	} else {
		$menuItem = &$menu->getItem($query['Itemid']);
	}
	
	
	$mView	= (empty($menuItem->query['view'])) ? null : $menuItem->query['view'];
	$mLayout	= (empty($menuItem->query['layout'])) ? null : $menuItem->query['layout'];
	$view = isset($query['view'])?$query['view']:$mView;
	
	if(isset($query['view'])){     
	   	 		
		if(empty($query['Itemid']) || ($query['view']!=$mView)) {
			$segments[] = $query['view'];
		}
		unset($query['view']);
	}
	
	if(isset($query['layout'])){
	    
	    if($query['layout']!='jaview' && $query['layout']!=$mLayout){
	        $segments[] = $query['layout'];
	    }
	    
		unset($query['layout']);
	}
	
	if(isset($query['uid'])){
	    
	    $temp = $query['uid'];
//	    $user = JFactory::getUser((int)$query['uid']);
//	    if (isset($user)) 
//	    	$temp = $user->username;
	    $segments[] = $temp;
		unset($query['uid']);
	}
		
	if(isset($query['cid'])) {
		if (!isset($query['alias'])){
			if (is_array($query['cid'])){
				$segments[] = implode(',',$query['cid']);
				
			}
			else{
				$segments[] = $query['cid'];
			}
			unset($query['cid']);
		}
		elseif (is_array($query['cid'])){
			$segments[] = implode(',',$query['cid']).'-'.$query['alias'];
			unset($query['alias']);
			unset($query['cid']);			
		}
		
		
	}
	elseif (isset($query['alias'])) unset($query['alias']);
	
	return $segments;
}

function JajobboardParseRoute($segments)
{ 
	$vars = array();
    //Get the active menu item
	$menu =& JSite::getMenu();
	$item =& $menu->getActive();
    // Count route segments
	$count = count($segments);
	//Standard routing for articles
	if(!isset($item)){
		$vars['view']  = $segments[0];
		$temp = explode(':', $segments[$count-1]);
		if($count == 3) {
			$vars['layout']  = $segments[$count-2];
			if(intval(@$temp[0])){
		        if($vars['layout']!='myjobs')
        			$vars['cid']    = array(@$temp[0]);
        		$vars['alias']    = @$temp[1];
		    }
		    else 
		    {
		    	if($vars['layout']=='myjobs') $vars['uid']    = @$temp[0];
		    }
		}
		elseif($count == 2) { 
			if(!intval(@$temp[0])){
				$db = JFactory::getDBO();
				if ($temp[0])
					$temp[0] = str_replace('()','.',$temp[0]);
				$query = "SELECT * FROM #__users WHERE username='".@$temp[0]."'";
				$db->setQuery($query);
				$result = $db->loadObject();
				if($result){
			        
			        if (!isset($vars['layout']))
			        	$vars['layout']  = 'jaview';
					if($vars['layout']=='myjobs') $vars['uid']    = @$temp[0];
	        		else 
	        			$vars['cid']    = array(@$temp[0]);
	        		
	        		$vars['alias']    = @$temp[1];
			    }
			    else{
			        $vars['layout']  = $segments[$count-1];
			    }
			}
			else 
			{
				$vars['layout']  = 'jaview';
		        if($vars['layout']=='myjobs') $vars['uid']    = @$temp[0];
        		else $vars['cid']    = array(@$temp[0]);
        		$vars['alias']    = @$temp[1];
			}
			
		}
		elseif($count==1){
		    if($vars['view']=='jaerrors') $vars['layout'] = 'default';
            elseif($segments[0]=='jaerrors'){
                $vars['view']='jaerrors';
                $vars['layout'] = 'default';
            }
            else {
                $vars['view'] = $segments[0];
            }
		}
//		print_r($vars);
		return $vars;
	}
	
	if($count == 1) {
		$vars['layout'] = 'jaview';
	}
    $vars['view'] = '';
    $temp = explode(':', $segments[$count-1]);
    if(intval(@$temp[0])){
    	if($count == 1) {
    	    $vars['view'] = $item->query['view'];
    	}            
    	elseif($count == 2) {
    		
	    	    $vars['view'] = $item->query['view'];
	    		$vars['layout']  = $segments[$count-2];
			
			
    	}
    	elseif ($count==3){
    	    $vars['view'] = $segments[$count-3];
    		$vars['layout']  = $segments[$count-2];
    	}
        
    	if($vars['layout']=='myjobs') $vars['uid']    = @$temp[0];
        else $vars['cid']    = array(@$temp[0]);
    	$vars['alias']    = @$temp[1];
    }
    elseif($count == 2){
    	$db = JFactory::getDBO();
    	$segments[$count-1] = str_replace('()','.',$segments[$count-1]);
		$query = "SELECT * FROM #__users WHERE username='".$segments[$count-1]."'";
		$db->setQuery($query);
		$result = $db->loadObject();
		
		if (!$result)
		{
	        $vars['view'] = $segments[$count-2];
	    	$vars['layout']  = $segments[$count-1];
	    }
		else 
		{
			$vars['cid']  = $result->username;
			$vars['uid']  = $result->username;
			
			if(file_exists(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'views'.DS.$item->query['view'].DS.'tmpl'.DS.$segments[$count-2].'.php')){
				$vars['view'] = $item->query['view'];
    			$vars['layout'] = $segments[$count-2];
			}
			elseif(is_dir(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'views'.DS.$segments[$count-2])){
				$vars['view'] = $segments[$count-2];
    			$vars['layout'] = 'jaview';
			}
			else{
				$vars['view'] = $item->query['view'];
    			$vars['layout'] = $item->query['layout'];
			}
			
		}
		
    }
    elseif($count==1){
        if(@$item->query['view']!='') {
            $vars['view'] = $item->query['view'];
            $vars['layout'] = $segments[$count-1];
        }
        else{
            $vars['view'] = $segments[$count-1];
        }
    }
	
    if($vars['view']=='jaerrors') $vars['layout'] = 'default';
    if($segments[0]=='jaerrors'){
        $vars['view']='jaerrors';
        $vars['layout'] = 'default';
    }
    unset($segments);
	
	return $vars;
	
}
